package icu.lookyousmileface.syncqueue;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author starrysky
 * @title: SyncQueueUse
 * @projectName Juc_Pro
 * @description: 同步队列
 * @date 2021/1/299:17 下午
 */
public class SyncQueueUse {
    public static void main(String[] args) throws InterruptedException {
        SynchronousQueue<String> syncQueue = new SynchronousQueue<>();

        //无须for循环会自动产生一种互喂模式
        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + ":" + "A入队列");
                syncQueue.put("A");

                System.out.println(Thread.currentThread().getName() + ":" + "B入队列");
                syncQueue.put("B");

                System.out.println(Thread.currentThread().getName() + ":" + "C入队列");
                syncQueue.put("C");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T1").start();


        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(Thread.currentThread().getName() + ":" + syncQueue.take() + "出队列");
                TimeUnit.SECONDS.sleep(1);
                System.out.println(Thread.currentThread().getName() + ":" + syncQueue.take() + "出队列");
                TimeUnit.SECONDS.sleep(1);
                System.out.println(Thread.currentThread().getName() + ":" + syncQueue.take() + "出队列");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T2").start();

    }
}
